//
//  ManagerSettingRootViewController.m
//  CPICMobileServiceApp
//
//  Created by sinosoft on 13-2-28.
//  Copyright (c) 2013年 lizhiwei. All rights reserved.
//

#import "ManagerSettingRootViewController.h"
#import "RCAlertViewUtil.h"
#import "NSString+SBJson.h"
#import "Config.h"
#import "AppDelegate.h"
#import "UIImageView+WebCache.h"
#import "IFServiceEvaluation.h"
#import "ASIFormDataRequest.h"

@interface ManagerSettingRootViewController ()

@end

@implementation ManagerSettingRootViewController
@synthesize TableViewUser,TableManager;
@synthesize txtUserAccount,txtUserName,txtManagerAccount,txtManagerName,txtManagerPhone;
@synthesize UserAccount,UserName;
@synthesize managerAccount,managerName,managerPhone;
@synthesize progress;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title=@"客户经理设置";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    NSUserDefaults *accountDefaults = [NSUserDefaults standardUserDefaults];
    NSString *bgimage= [accountDefaults objectForKey:@"UserDefaultBgKindKey"];
    
    if ([bgimage isEqualToString:@"蓝色主题风格"]) 
    {
        ADD_BACKGROUND_BUTTONITEM(@"icon640X88@2x.png");
        ADD_LEFT_BUTTONITEM(@"back.png");
        ADD_TITLE_NAMEITEM(@"客户经理设置");
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg640X773@2x"]];
    }
    else if([bgimage isEqualToString:@"棕色主题风格"])
    {
        ADD_BACKGROUND_BUTTONITEM(@"icon640X88_01@2x.png");
        ADD_LEFT_BUTTONITEM(@"back_01.png");
        ADD_TITLE_NAMEITEM(@"客户经理设置");
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg640X773_01@2x"]];
    }
    else if([bgimage isEqualToString:@"重大节日皮肤"])
    {
        NSData *dataImg_02=[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_DATA_02];
        NSLog(@"-------找到%@",dataImg_02);
        NSData *dataImg_03=[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_DATA_03];
        NSData *dataImg_04=[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_DATA_04];
        
        NSURL *urlSkinImg_02=[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_URL_02]];
        NSURL *urlSkinImg_03=[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_URL_03]];
        NSURL *urlSkinImg_04=[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_URL_04]];
        NSLog(@"-------找到333%@",urlSkinImg_02);
        
        UIImageView * LogoImage = [[UIImageView alloc]init];
        [LogoImage setImageWithURL:urlSkinImg_03 success:^(UIImage *image)
         {
             NSData *imgData=UIImagePNGRepresentation(LogoImage.image);
             [[NSUserDefaults standardUserDefaults] setObject:imgData forKey:SKIN_IMG_DATA_03];
         } failure:^(NSError *error)
         {
             [self justShowMessage:@"更新皮肤失败!" title:@"提示"];
         }];
        
        LogoImage.image = [UIImage imageWithData:dataImg_03];
        LogoImage.frame = CGRectMake(0, 0, LogoImage.image.size.width/2, LogoImage.image.size.height/2);
        [self.view addSubview:LogoImage];
        
        //--------------------------
        UIButton * CallOutBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
        CallOutBtn1.frame = CGRectMake(10, 10, 109/2, 57/2);
        UIImageView * Back = [[UIImageView alloc]init];
        [Back setImageWithURL:urlSkinImg_02 success:^(UIImage *image)
         {
             NSData *imgData=UIImagePNGRepresentation(Back.image);
             [[NSUserDefaults standardUserDefaults] setObject:imgData forKey:SKIN_IMG_DATA_02];
         } failure:^(NSError *error)
         {
             [self justShowMessage:@"更新皮肤失败!" title:@"提示"];
         }];
        
        [Back setImage:[UIImage imageWithData:dataImg_02]];
        [CallOutBtn1 setImage:Back.image forState:UIControlStateNormal];
        [CallOutBtn1 addTarget:self action:@selector(ClickCallOutBtn) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:CallOutBtn1];
        //--------------------------
        
        ADD_TITLE_NAMEITEM(@"客户经理设置");
        
        
        UIImageView *bgimage = [[UIImageView alloc]initWithFrame:CGRectMake(0,44, self.view.frame.size.width, self.view.frame.size.height-44)];
        [bgimage setImageWithURL:urlSkinImg_04 success:^(UIImage *image)
         {
             NSData *imgData=UIImagePNGRepresentation(bgimage.image);
             [[NSUserDefaults standardUserDefaults] setObject:imgData forKey:SKIN_IMG_DATA_04];
         } failure:^(NSError *error)
         {
             [self justShowMessage:@"更新皮肤失败!" title:@"提示"];
         }];
        
        [bgimage setImage:[UIImage imageWithData:dataImg_04]];
        [self.view addSubview:bgimage];
        [bgimage release];
    }
    
    //添加UITableview
    TableViewUser = [[UITableView alloc]initWithFrame:CGRectMake(5, 62, self.view.frame.size.width, 200) style:UITableViewStyleGrouped];
	TableViewUser.delegate = self;
	TableViewUser.dataSource = self;
    
    TableViewUser.opaque = NO;
    TableViewUser.backgroundView = nil;
    TableViewUser.autoresizingMask = UIViewContentModeTop;
    TableViewUser.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    TableViewUser.scrollEnabled=NO;
    TableViewUser.backgroundColor=[UIColor clearColor];
    //[TableViewUser reloadData];
	[self.view addSubview:TableViewUser];
    
    
    TableManager = [[UITableView alloc]initWithFrame:CGRectMake(5, 194, self.view.frame.size.width, 250) style:UITableViewStyleGrouped];
	TableManager.delegate = self;
	TableManager.dataSource = self;
    
    TableManager.opaque = NO;
    TableManager.backgroundView = nil;
    TableManager.autoresizingMask = UIViewContentModeTop;
    TableManager.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    TableManager.scrollEnabled=NO;
    TableManager.backgroundColor=[UIColor clearColor];
    //[TableViewPassword reloadData];
	[self.view addSubview:TableManager];
    
    txtManagerAccount=[[UITextField alloc]initWithFrame:CGRectMake(125, 4.5, 148, 31)];
    txtManagerAccount.borderStyle=UITextBorderStyleRoundedRect;
    txtManagerAccount.returnKeyType=UIReturnKeyDone;
    txtManagerAccount.delegate=self;
    txtManagerAccount.font = [UIFont systemFontOfSize:14.0];
    
    txtManagerName=[[UITextField alloc]initWithFrame:CGRectMake(125, 4.5, 148, 31)];
    txtManagerName.borderStyle=UITextBorderStyleRoundedRect;
    txtManagerName.returnKeyType=UIReturnKeyDone;
    txtManagerName.delegate=self;
    txtManagerName.font = [UIFont systemFontOfSize:14.0];
    
    txtManagerPhone=[[UITextField alloc]initWithFrame:CGRectMake(125, 4.5, 148, 31)];
    txtManagerPhone.borderStyle=UITextBorderStyleRoundedRect;
    txtManagerPhone.returnKeyType=UIReturnKeyDone;
    CustomNumbleKeyboardView *customKeyboardView = [[CustomNumbleKeyboardView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 238)];
    customKeyboardView.delegate = self;
    [txtManagerPhone setInputView:customKeyboardView];
    txtManagerPhone.delegate =self;
    txtManagerPhone.font = [UIFont systemFontOfSize:14.0];
    
    UIButton *btnRegister=[UIButton buttonWithType:UIButtonTypeCustom];
     [btnRegister setImage:[UIImage imageNamed:@"SaveSet.png"] forState:UIControlStateNormal];
    [btnRegister setTitle:@"保存设置" forState:UIControlStateNormal];
    btnRegister.frame=CGRectMake((self.view.frame.size.width-118)/2, 380, 310/2, 68/2);
    [btnRegister addTarget:self action:@selector(saveSetting) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnRegister];
    
    UILabel * lbSuggestion = [[UILabel alloc]initWithFrame:CGRectMake((self.view.frame.size.width-118)/2, 380,  310/2, 68/2)];
    lbSuggestion.text = @"保存设置";
    lbSuggestion.textAlignment = UITextAlignmentCenter;
    lbSuggestion.textColor = [UIColor whiteColor];
    lbSuggestion.backgroundColor = [UIColor clearColor];
    [self.view addSubview:lbSuggestion];
    [lbSuggestion release];
    
    arrUser=[[NSMutableArray alloc]initWithObjects:@"用户账号:",@"用户名称:", nil];
    arrManager=[[NSMutableArray alloc]initWithObjects:@"客户经理工号:",@"客户经理姓名:",@"客户经理手机号:", nil];
    
    NSDictionary * dictManagerInfo =[[NSUserDefaults standardUserDefaults] dictionaryForKey:DICT_MANAGER];
    NSLog(@"DICT_MANAGER%@",dictManagerInfo);
    if (dictManagerInfo!=nil)
    {
        if ([dictManagerInfo valueForKey:@"ManagerNum"]) {
            self.txtManagerAccount.text=[dictManagerInfo valueForKey:@"ManagerNum"];
        }
        if ([dictManagerInfo valueForKey:@"ManagerName"]) {
            self.txtManagerName.text=[dictManagerInfo valueForKey:@"ManagerName"];
        }
        if ([dictManagerInfo valueForKey:@"ManagerPhone"]) {
            self.txtManagerPhone.text=[dictManagerInfo valueForKey:@"ManagerPhone"];
        }
    }
    else
    {
        
    }

    [self setRequest];
    
    [self setEvaluationRequest];
}

-(void) ClickCallOutBtn
{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)saveSetting
{
    if ([self.txtManagerAccount.text isEqualToString:@""]||
        [self.txtManagerName.text isEqualToString:@""]||[self.txtManagerPhone.text isEqualToString:@""])
    {
        [self justShowMessage:@"请输入完整的客户经理信息!" title:@"提示"];
        return;
    }
    
    NSString * Mobile = txtManagerPhone.text;
    if (![self isValidateMobile:Mobile])
    {
        [self justShowMessage:@"非法的手机号码,请输入正确的手机号码" title:@""];
        return;
    }
    
    [self setManagerInfoRequest];
    //[self justShowMessage:@"保存设置成功！" title:@"提示"];
}

-(void)setRequest//向太保移动平台发送请求报文
{
    NSDictionary *dictUserInfo=[[NSUserDefaults standardUserDefaults] dictionaryForKey:DICT_USER_INFO];
    if (dictUserInfo!=nil) 
    {
        if ([dictUserInfo valueForKey:@"UserAccount"]) 
        {
            NSLog(@"%@",[dictUserInfo valueForKey:@"UserAccount"]);
            self.UserAccount = [dictUserInfo valueForKey:@"UserAccount"];
        }
        
//        if ([dictUserInfo valueForKey:@"UserName"]) 
//        {
//            self.UserName = [dictUserInfo valueForKey:@"UserName"];
//        }
    }
    else
    {
        [self justShowMessage:@"用户还没登录,请登录系统" title:@"注意"];
        return;
    }

    NSDictionary *dictCustomerInfo=[[NSUserDefaults standardUserDefaults] dictionaryForKey:DICT_CUSTOMER_INFO];
    if (dictCustomerInfo!=nil)
    {
        if ([dictCustomerInfo valueForKey:@"userName"])
        {
            self.UserName= [dictCustomerInfo valueForKey:@"userName"];
            NSLog(@"UserName:%@",UserName);
        }
    }
    else
    {
        //[self justShowMessage:@"用户还没登录,请登录系统" title:@"注意"];
    }
   // NSString *strRequestCode=@"RequestCode=61801";
    NSString *strRequestData=[NSString stringWithFormat:@"{\"userAccount\":\"%@\",\"userName\":\"%@\"}",self.UserAccount,self.UserName];
    NSLog(@"%@",strRequestData);
    //NSString *strUrl=[NSString stringWithFormat:@"%@%@%@",Server_Url,strRequestCode,strRequestData];
    //NSLog(@"++++++++++++++%@",strUrl);
    //strUrl = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//字符串编码转换
    //NSURL *myurl = [NSURL URLWithString:strUrl];//This method expects URLString to contain any necessary percent escape codes, which are ‘:’, ‘/’, ‘%’, ‘#’, ‘;’, and ‘@’. Note that ‘%’ escapes are translated via UTF-8.
    //NSLog(@"URL:%@",myurl);
    
    ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:Server_Url_Form]];
    
    //设置需要POST的数据，这里提交两个数据，A=a&B=b
    [request setPostValue:@"61801" forKey:@"RequestCode"];
    [request setPostValue:strRequestData forKey:@"RequestData"];
    [request setDelegate:self];
    [request setDidFailSelector:@selector(ResponseFail:)];
    [request setDidFinishSelector:@selector(ResonseSuccess:)];
    [request startAsynchronous];
    
    showProgress = YES;
    if (showProgress) {
        NSLog(@"88888");
        // [NSThread sleepForTimeInterval:1];
        progress= [[MBProgressHUD alloc]initWithView:self.view];
        [self.view addSubview:progress];
        [self.view bringSubviewToFront:progress];
        progress.labelText = @"查询中...";
        [progress show:YES];
        showProgress = NO;
    };
}

- (void)ResonseSuccess:(ASIHTTPRequest *)request
{
    if (!showProgress)
    {
        [progress removeFromSuperview];
        progress = nil;
    }
    NSString *responseString = [request responseString];
    NSLog(@"responseString------%@",responseString);
    NSMutableDictionary *dictResponse=[responseString JSONValue];
    NSMutableDictionary *jsonoSubObj = [dictResponse objectForKey:@"head"];
    NSString * ss = [[NSString alloc] initWithFormat:@"%@",[jsonoSubObj objectForKey:@"success"]];
    
    if ([ss isEqualToString:@"1"])
    {
        NSMutableDictionary * managerInfo = [dictResponse objectForKey:@"userManagerInfoList"];
        NSArray * ManagerAccountArray = [managerInfo objectForKey:@"data"];
        
        if ([ManagerAccountArray count] == 0)
        {
            return;
        }
        
        if ([ManagerAccountArray count] == 1)
        {
            NSMutableDictionary * ManagerAccount =[ManagerAccountArray objectAtIndex:0];
            self.managerAccount = [[NSString alloc] initWithFormat:@"%@",[ManagerAccount objectForKey:@"managerAccount"]];
            self.managerName = [[NSString alloc] initWithFormat:@"%@",[ManagerAccount objectForKey:@"managerName"]];
            self.managerPhone = [[NSString alloc] initWithFormat:@"%@",[ManagerAccount objectForKey:@"managerPhone"]];
            
            //填充到文本框中
            txtManagerAccount.text = self.managerAccount;
            txtManagerName.text = self.managerName;
            txtManagerPhone.text = self.managerPhone;
            IfDetailDisclosureButton = FALSE;
        }
        else
        {
            for (int k = 0; k<[ManagerAccountArray count]; k++)
            {
                NSMutableDictionary * ManagerAccount =[ManagerAccountArray objectAtIndex:k];
                self.managerAccount = [[NSString alloc] initWithFormat:@"%@",[ManagerAccount objectForKey:@"managerAccount"]];
                self.managerName = [[NSString alloc] initWithFormat:@"%@",[ManagerAccount objectForKey:@"managerName"]];
                self.managerPhone = [[NSString alloc] initWithFormat:@"%@",[ManagerAccount objectForKey:@"managerPhone"]];
                [arrmanagerAccount addObject:self.managerAccount];
                [arrmanagerName addObject:self.managerName];
                [arrmanagerPhone addObject:self.managerPhone];
            }
            NSLog(@"===========%@",arrmanagerAccount);
            
            //填充到文本框中
            txtManagerAccount.text = self.managerAccount;
            txtManagerName.text = self.managerName;
            txtManagerPhone.text = self.managerPhone;
            IfDetailDisclosureButton = FALSE;
        }
        
        NSDictionary *dictManageInfo=[[NSDictionary alloc]initWithObjectsAndKeys:txtManagerAccount.text,@"ManagerNum",txtManagerName.text,@"ManagerName",txtManagerPhone.text,@"ManagerPhone", nil];
        NSUserDefaults *myDefaults=[NSUserDefaults standardUserDefaults];
        [myDefaults setObject:dictManageInfo forKey:DICT_MANAGER];
        NSLog(@"DICT_MANAGER%@",dictManageInfo);
        
        NSDictionary * dictManagerInfo =[[NSUserDefaults standardUserDefaults] dictionaryForKey:DICT_MANAGER];
        NSLog(@"DICT_MANAGER%@",dictManagerInfo);
        if (dictManagerInfo!=nil)
        {
            if ([dictManagerInfo valueForKey:@"ManagerNum"]) {
                self.txtManagerAccount.text=[dictManagerInfo valueForKey:@"ManagerNum"];
            }
            if ([dictManagerInfo valueForKey:@"ManagerName"]) {
                self.txtManagerName.text=[dictManagerInfo valueForKey:@"ManagerName"];
            }
            if ([dictManagerInfo valueForKey:@"ManagerPhone"]) {
                self.txtManagerPhone.text=[dictManagerInfo valueForKey:@"ManagerPhone"];
            }
        }
        else
        {
            
        }
    }
    else
    {
        //[self justShowMessage:@"获取客户经理信息失败！" title:@"提示"];
        return;
    }
}

- (void)ResponseFail:(ASIHTTPRequest *)request
{
    if (!showProgress)
    {
        [progress removeFromSuperview];
        progress = nil;
    }
    NSLog(@"服务器连接失败!");
    [self justShowMessage:@"服务器连接失败！" title:@"提示"];
}


//向太保移动平台发送客户经理设置信息请求报文
-(void) setManagerInfoRequest
{
    //NSString *strRequestCode=@"RequestCode=60601";
    NSString *strRequestData=[NSString stringWithFormat:@"{\"userAccount\":\"%@\",\"userName\":\"%@\",\"managerNum\":\"%@\",\"managerName\":\"%@\",\"managerPhone\":\"%@\"}",self.UserAccount,self.UserName,self.txtManagerAccount.text,self.txtManagerName.text,self.txtManagerPhone.text];
    NSLog(@"客户经理设置请求报文:%@",strRequestData);
//    NSString *strUrl=[NSString stringWithFormat:@"%@%@%@",Server_Url,strRequestCode,strRequestData];
//    NSLog(@"++++++++++++++%@",strUrl);
//    strUrl = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//字符串编码转换
//    NSURL *myurl = [NSURL URLWithString:strUrl];//This method expects URLString to contain any necessary percent escape codes, which are ‘:’, ‘/’, ‘%’, ‘#’, ‘;’, and ‘@’. Note that ‘%’ escapes are translated via UTF-8.
//    NSLog(@"URL:%@",myurl);
    ASIFormDataRequest *request1 = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:Server_Url_Form]];
    
    //设置需要POST的数据，这里提交两个数据，A=a&B=b
    [request1 setPostValue:@"60601" forKey:@"RequestCode"];
    [request1 setPostValue:strRequestData forKey:@"RequestData"];
    [request1 setDelegate:self];
    [request1 setDidFailSelector:@selector(ManagerInfoResponseFail:)];
    [request1 setDidFinishSelector:@selector(ManagerInfoResonseSuccess:)];
    [request1 startAsynchronous];
    
    showProgress = YES;
    if (showProgress) {
        NSLog(@"88888");
        // [NSThread sleepForTimeInterval:1];
        progress= [[MBProgressHUD alloc]initWithView:self.view];
        [self.view addSubview:progress];
        [self.view bringSubviewToFront:progress];
        progress.labelText = @"查询中...";
        [progress show:YES];
        showProgress = NO;
    };
}

- (void)ManagerInfoResonseSuccess:(ASIHTTPRequest *)request1
{
    if (!showProgress)
    {
        [progress removeFromSuperview];
        progress = nil;
    }
    NSString *responseString = [request1 responseString];
    NSLog(@"responseString %@",responseString);
    NSMutableDictionary *dictResponse=[responseString JSONValue];
    NSMutableDictionary *jsonoSubObj = [dictResponse objectForKey:@"head"];
    NSString * ss = [[NSString alloc] initWithFormat:@"%@",[jsonoSubObj objectForKey:@"success"]];
    if ([ss isEqualToString:@"1"])
    {
        NSDictionary *dictManageInfo=[[NSDictionary alloc]initWithObjectsAndKeys:txtManagerAccount.text,@"ManagerNum",txtManagerName.text,@"ManagerName",txtManagerPhone.text,@"ManagerPhone", nil];
        NSUserDefaults *myDefaults=[NSUserDefaults standardUserDefaults];
        [myDefaults setObject:dictManageInfo forKey:DICT_MANAGER];
        
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"保存设置成功！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
        alert.delegate = self;
        [alert release];
        return;
    }
    else
    {
        [self justShowMessage:@"保存设置失败！" title:@"提示"];
        return;
    }
    return;
}

- (void)ManagerInfoResponseFail:(ASIHTTPRequest *)request1
{
    if (!showProgress)
    {
        [progress removeFromSuperview];
        progress = nil;
    }
    NSLog(@"服务器连接失败!");
    [self justShowMessage:@"服务器连接失败！" title:@"提示"];
}

- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch(alert.tag)
    {
        case 0:
            if (buttonIndex == [alert cancelButtonIndex])
                //默认cancelButtonIndex = 0，每个按钮index可设
            {
//                NSString *serviceid =@"60601";
//                NSString *servicename =@"客户经理设置";
//                IFServiceEvaluation *ifserviceevaluation =[[IFServiceEvaluation alloc]initView:serviceid withserviceName:servicename];
//                [ifserviceevaluation setIfServiceEvaluationRequest];
//                [ifserviceevaluation setHidden:YES];
//                [self.view addSubview:ifserviceevaluation];
//                [ifserviceevaluation release];
                [self IFServiceEvaluation];
            }
            break;
        case 1:
            return;
        default:
            break;
    }
}
-(void)setEvaluationRequest
{
    NSLog(@"==================");
    NSString *serviceid =@"60601";
    NSString *servicename =@"客户经理设置";
    IFServiceEvaluation *ifserviceevaluation =[[IFServiceEvaluation alloc]initView:serviceid withserviceName:servicename];
    [ifserviceevaluation setIfServiceEvaluationRequest];
    [ifserviceevaluation setHidden:YES];
    [self.view addSubview:ifserviceevaluation];
    [ifserviceevaluation release];
}
-(void) IFServiceEvaluation
{
    NSString *serviceid =@"60601";
    NSString *servicename =@"客户经理设置";
    NSUserDefaults *myDefaults=[NSUserDefaults standardUserDefaults];
    if ([myDefaults stringForKey:serviceid]) {
        NSString *strEvaluate=[myDefaults stringForKey:serviceid];
        if ([strEvaluate isEqualToString:@"1"]) {
            AddServiceEvaluationViewController *serviceevluation = [[AddServiceEvaluationViewController alloc]initView:serviceid withserviceName:servicename];
            [[AppDelegate app].unc pushViewController:serviceevluation animated:YES];
            [serviceevluation release];
        }
        else
        {
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
    }
    else
    {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

#pragma UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(tableView==TableViewUser)
    {
        return [arrUser count];
    }
    else
    {
        return [arrManager count];
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(tableView==TableViewUser)
    {
        static NSString *Identifier=@"Identifier";
        UITableViewCell *cell=(UITableViewCell *)[TableViewUser dequeueReusableCellWithIdentifier:Identifier];
        if (cell==nil)
        {
            cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier]autorelease];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.selectionStyle=UITableViewCellSelectionStyleNone;//选中后颜色没变化
            
            if(indexPath.row==0)
            {
                UILabel *lblUserAccount=[[UILabel alloc]initWithFrame:CGRectMake(15, 9.5, 100, 21)];
                lblUserAccount.backgroundColor=[UIColor clearColor];
                lblUserAccount.text=[arrUser objectAtIndex:indexPath.row];
                lblUserAccount.font = [UIFont systemFontOfSize:14.0];
                [cell addSubview:lblUserAccount];
                [lblUserAccount release];
                
                txtUserAccount=[[UITextField alloc]initWithFrame:CGRectMake(125, 4.5, 170, 31)];
                txtUserAccount.borderStyle=UITextBorderStyleLine;
                txtUserAccount.enabled=NO;
                txtUserAccount.delegate = self;
                txtUserAccount.font = [UIFont systemFontOfSize:14.0];
                [cell addSubview:txtUserAccount];
                
                txtUserAccount.placeholder = self.UserAccount;
            }
            else if(indexPath.row==1)
            {
                UILabel *lblUserName=[[UILabel alloc]initWithFrame:CGRectMake(15,9.5, 110, 21)];
                lblUserName.backgroundColor=[UIColor clearColor];
                lblUserName.text=[arrUser objectAtIndex:indexPath.row];
                lblUserName.font = [UIFont systemFontOfSize:14.0];
                [cell addSubview:lblUserName];
                [lblUserName release];
                
                txtUserName=[[UITextField alloc]initWithFrame:CGRectMake(125, 4.5, 170, 31)];
                txtUserName.borderStyle=UITextBorderStyleLine;
                txtUserName.delegate = self;
                txtUserName.enabled=NO;
                txtUserName.font = [UIFont systemFontOfSize:14.0];
                [cell addSubview:txtUserName];
                
                txtUserName.placeholder = self.UserName;
            }

        }
        return cell;
    }
    else
    {
        static NSString *Identifier=@"Identifier";
        UITableViewCell *cell=(UITableViewCell *)[TableViewUser dequeueReusableCellWithIdentifier:Identifier];
        if (cell==nil)
        {
            cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier]autorelease];
            if (IfDetailDisclosureButton)
            {
                cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
            }
            else
            {
                cell.accessoryType = UITableViewCellAccessoryNone;
            }
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            
            if(indexPath.row==0)
            {
                UILabel *lblUserPassword=[[UILabel alloc]initWithFrame:CGRectMake(15, 9.5, 116, 21)];
                lblUserPassword.backgroundColor=[UIColor clearColor];
                lblUserPassword.text=[arrManager objectAtIndex:indexPath.row];
                lblUserPassword.font = [UIFont systemFontOfSize:14.0];
                [cell addSubview:lblUserPassword];
                [lblUserPassword release];
                

                [cell addSubview:txtManagerAccount];
            }
            else if(indexPath.row==1)
            {
                UILabel *lblUserIdenPassword=[[UILabel alloc]initWithFrame:CGRectMake(15, 9.5, 119, 21)];
                lblUserIdenPassword.backgroundColor=[UIColor clearColor];
                lblUserIdenPassword.text=[arrManager objectAtIndex:indexPath.row];
                lblUserIdenPassword.font = [UIFont systemFontOfSize:14.0];
                [cell addSubview:lblUserIdenPassword];
                [lblUserIdenPassword release];
                

                [cell addSubview:txtManagerName];
            }
            else if(indexPath.row==2)
            {
                UILabel *lblIdenCode=[[UILabel alloc]initWithFrame:CGRectMake(15, 9.5, 120, 21)];
                lblIdenCode.backgroundColor=[UIColor clearColor];
                lblIdenCode.text=[arrManager objectAtIndex:indexPath.row];
                lblIdenCode.font = [UIFont systemFontOfSize:14.0];
                [cell addSubview:lblIdenCode];
                [lblIdenCode release];
                

                [cell addSubview:txtManagerPhone];
            }
        }
        return cell;
    }
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

//数字键盘
- (void)numberKeyBoardInput:(NSInteger) number
{
    NSMutableString *textString = [[[NSMutableString alloc] initWithFormat:@"%@%d",txtManagerPhone.text,number] autorelease];
    if ([textString length]>11)
    {
        [self justShowMessage:@"手机号码输入不能超过11位!" title:@""];
        return;
    }
    txtManagerPhone.text = textString;
}

- (void)numberKeyBoardBackspace
{
    NSMutableString* mutableString = [[[NSMutableString alloc] initWithFormat:@"%@", txtManagerPhone.text] autorelease];
    if ([mutableString length] >=1) {
        NSRange tmpRange;
        tmpRange.location = [mutableString length] - 1;
        tmpRange.length = 1;
        [mutableString deleteCharactersInRange:tmpRange];
    }
    txtManagerPhone.text = mutableString;
    
}

- (void)numberKeyBoardFinish
{
    [txtManagerPhone resignFirstResponder];
}

/*手机号码验证*/
-(BOOL) isValidateMobile:(NSString *)mobile
{
    //手机号以13， 15，18开头，八个 \d 数字字符
    NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:mobile];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if(textField==txtManagerAccount)
    {
        [self SetAnimation:80];
    }
    else if(textField==txtManagerName)
    {
        [self SetAnimation:100];
    }
    else if(textField==txtManagerPhone)
    {
        [self SetAnimation:120];
    }
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if(textField==txtManagerAccount)
    {
        [self backAnimation:80];
    }
    else if(textField==txtManagerName)
    {
        [self backAnimation:100];
    }
    else if(textField==txtManagerPhone)
    {
        [self backAnimation:120];
    }
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if(textField==txtUserAccount)
    {
        if(range.location>=50)
        {
            return NO;
        }
        else
        {
            return YES;
        }
    }
    else if(textField==txtUserName)
    {
        if(range.location>=20)
        {
            return NO;
        }
        else
        {
            return YES;
        }
    }
    else if(textField==txtManagerAccount)
    {
        if(range.location>=10)
        {
            return NO;
        }
        else
        {
            return YES;
        }
    }
    else if(textField==txtManagerName)
    {
        if(range.location>=20)
        {
            return NO;
        }
        else
        {
            return YES;
        }
    }
    else if(textField==txtManagerPhone)
    {
        if(range.location>=20)
        {
            return NO;
        }
        else
        {
            return YES;
        }
    }
    
    return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

-(void)SetAnimation:(CGFloat)Xorigin
{
    NSTimeInterval animationDuration = 0.30f;
    CGRect frame = self.view.frame;
    //frame.origin.x+=Xorigin;
    frame.origin.y-=Xorigin;
    //frame.size.height +=Xorigin;
    self.view.frame = frame;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    self.view.frame = frame;
    [UIView commitAnimations];
}

-(void)backAnimation:(CGFloat)Xorigin
{
    NSTimeInterval animationDuration = 0.30f;
    CGRect frame = self.view.frame;
    //frame.origin.x -=Xorigin;
    frame.origin.y+=Xorigin;
    //frame.size.height -=Xorigin;
    self.view.frame = frame;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    self.view.frame = frame;
    [UIView commitAnimations];
    // [textField resignFirstResponder];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)dealloc
{
    [progress release];
    [txtUserAccount release];
    [txtUserName release];
    [txtManagerAccount release];
    [txtManagerName release];
    [txtManagerPhone release];
    [arrUser release];
    [arrManager release];
    [super dealloc];
}

@end
